/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rsds;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author ahmet
 */
public class SubsystemAddPanel extends JPanel{
    private int subsystemIndex;
    private JLabel subsystemIndexLabel;
    private String subsystemEquation;
    private JTextField subsystemEquationTextField;

    private JButton removeButton;

    private SubsystemContainerPanel container;
    private final Vector<SubsystemAddPanel> subsystems;

    public SubsystemAddPanel(final SubsystemContainerPanel container, final Vector<SubsystemAddPanel> subsystems, int index) {
        super();

        this.container = container;
        this.subsystems = subsystems;
        subsystemIndex = index;

        setLayout(new BorderLayout());

        subsystemIndexLabel = new JLabel(""+subsystemIndex);
        add(subsystemIndexLabel, BorderLayout.WEST);

        subsystemEquationTextField = new JTextField();
        subsystemEquationTextField.setColumns(20);
        add(subsystemEquationTextField, BorderLayout.CENTER);

        removeButton = new JButton("x"); // if removed, then remove from subsystems
        removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                subsystems.removeElementAt(subsystemIndex-1);
                if (subsystemIndex-1 < subsystems.size()) {
                    for(int i=subsystemIndex-1; i<subsystems.size(); i++) {
                        subsystems.elementAt(i).changeSubsystemIndex(i+1);
                    }
                }
                container.fillContainer();
            }
        });

        add(removeButton, BorderLayout.EAST);
    }

    public void changeSubsystemIndex(int newIndex) {
        subsystemIndex = newIndex;
        subsystemIndexLabel.setText(""+subsystemIndex);
    }

    public void setEnabled(boolean enabled) {
        subsystemEquationTextField.setEnabled(enabled);
        removeButton.setEnabled(enabled);
    }

    public String getEquation() {
        return subsystemEquationTextField.getText();
    }
}
